﻿/*************************************
Function：文件，压缩处理
 Version：1.0
    Date：2012-04-28
  Author：rjdong
   Email：ooxxab@gmail.com
 WebSite：http://emake.info
*************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Yahoo.Yui.Compressor.MsBuildTask;
using Yahoo.Yui.Compressor;
using System.Globalization;
using Wind.Compressor.Entity;

namespace Wind.Compressor.Business
{
  
    public sealed class YuiBusiness : BaseCompressor
    {

        private YuiBusiness() { }
        public static readonly YuiBusiness Instance = new YuiBusiness();


        #region Compress Business Public Mothed
        /// <summary>
        /// 快速模式压缩
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public string QuickCompress(string fileName, string strContent)
        {
            string result = string.Empty;
            try
            {
                string filename = fileName;
                string name = Path.GetFileNameWithoutExtension(filename);
                string ext = Path.GetExtension(filename).ToLower();

                if (ext == ".css")
                    result = CssCompress(strContent);
                else if (ext == ".js")
                    result = JsCompress(strContent);
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.FileBusiness.QuickCompress error", ex);
            }
            return result;
        }

        /// <summary>
        /// 批压缩
        /// </summary>
        /// <param name="fileRootPath"></param>
        /// <param name="fileMidPath"></param>
        /// <param name="fileName"></param>
        /// <param name="setting"></param>
        /// <param name="destPath"></param>
        /// <returns></returns>
        public string DoCompressBat(string fileRootPath, string fileMidPath, string fileName, SettingInfo setting, string destPath)
        {
            string result = string.Empty;

            string fullFileName = string.Empty;
            string strContent = string.Empty;
            try
            {
                if (!fileRootPath.EndsWith("\\"))
                    fileRootPath += "\\";
                if (!fileMidPath.EndsWith("\\"))
                {
                    if (!string.IsNullOrEmpty(fileMidPath))
                        fileMidPath += "\\";
                }
                if (!destPath.EndsWith("\\"))
                    destPath += "\\";

                string name = Path.GetFileNameWithoutExtension(fileName);
                string ext = Path.GetExtension(fileName).ToLower();

                fullFileName = fileRootPath + fileMidPath + fileName;
                Encoding encodingCssSet = Utils.GetEncoding(setting.CssEncodeName);
                Encoding encodingJsSet = Utils.GetEncoding(setting.JsEncodeName);
                Encoding encodingFile = null;
                strContent = Utils.ReadFile(fullFileName, out encodingFile);
                if (encodingCssSet == null)
                {
                    if (encodingFile == null)
                        setting.CssEncodeName = Encoding.UTF8.EncodingName;
                    else
                        setting.CssEncodeName = encodingFile.EncodingName;
                }
                else
                {
                    setting.CssEncodeName = encodingCssSet.EncodingName;
                }
                if (encodingJsSet == null)
                {
                    if (encodingFile == null)
                        setting.JsEncodeName = Encoding.UTF8.EncodingName;
                    else
                        setting.JsEncodeName = encodingFile.EncodingName;
                }
                else
                {
                    setting.JsEncodeName = encodingJsSet.EncodingName;
                }
                destPath += fileMidPath;

                result = DoCompress(fullFileName, strContent, setting, destPath);
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.FileBusiness.CompressBat error,fullFileName=" + fullFileName, ex);
            }
            return result;
        }


        #endregion
    }
}
